﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using GameObjects;

namespace Utils
{
    public class SerialiseHelper
    {
        // Serialize to a base 64 string
        public static byte[] SerializeToBase64(object o)
        {
            var ms = new MemoryStream();
            var bf = new BinaryFormatter();
            bf.Serialize(ms, o);
            return ms.GetBuffer();
            //var bytes = ms.GetBuffer();

            //return bytes; //Convert.ToBase64String(bytes, 0, bytes.Length, Base64FormattingOptions.None);
        }

        // Deserialize to an object
        public static object DeserializeToObject(byte[] o)
        {
            //var bytes = Convert.FromBase64String(o);
            var ms = new MemoryStream(o);
            var bf = new BinaryFormatter();

            return bf.Deserialize(ms);
        }

        public static byte[] SerialiseAndCompress(object o)
        {
            return CompressHelper.Compress(SerializeToBase64(o));
        }

        public static object DeserialiseAndUncompress(byte[] o)
        {
            return DeserializeToObject(CompressHelper.Decompress(o));
        }
    }
}
